package com.lzl.mall.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzl.mall.order.dao.DetailDao;
import com.lzl.mall.order.dao.WalletDao;
import com.lzl.mall.order.entity.Detail;
import com.lzl.mall.order.entity.R;
import com.lzl.mall.order.entity.UserVO;
import com.lzl.mall.order.entity.Wallet;
import com.lzl.mall.order.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.Date;
import java.util.UUID;

@Service("MalletService")
public class WalletServiceImpl extends ServiceImpl<WalletDao, Wallet> implements WalletService {

    @Autowired
    private DetailServiceImpl detailService;


    @Override
    public Wallet query(String user) {
        Wallet wallet = this.getOne(new QueryWrapper<Wallet>()
                .eq("username", user));
         if(wallet!=null){
             return wallet;
         }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R consume(UserVO userVO) {
        String userName = userVO.getUserName();
        Double price = userVO.getPrice();
        Wallet wallet = this.getOne(new QueryWrapper<Wallet>()
                .eq("username", userName));
        Double bal = wallet.getBalance();
        if(wallet==null){
            return R.error().put("fail","钱包不存在该用户");
        }
        if(price > bal){
            return R.error().put("fail","钱包余额不足");
        }
        Double balance = bal - price;
        wallet.setBalance(balance);
        this.updateById(wallet);
        //做明细记录 如果项目是微服务项目，这里可用分布式事务解决事务回滚问题
//        这里我就直接做插入记录 有错误就回滚
        Detail detail = new Detail();
        detail.setCreateTime(new Date());
        detail.setDetailType(userVO.getType());
        detail.setUsername(wallet.getUsername());
        detail.setPrice(price);
//        int i= 1/0;
        detailService.save(detail);
        return R.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R refund(UserVO userVO) {
        String userName = userVO.getUserName();
        Double price = userVO.getPrice();
        Wallet wallet = this.getOne(new QueryWrapper<Wallet>()
                .eq("username", userName));
        Double bal = wallet.getBalance();
        if(wallet==null){
            return R.error().put("fail","钱包不存在该用户");
        }

        Double balance = bal  + price;
        wallet.setBalance(balance);
        this.updateById(wallet);
        //做明细记录 如果项目是微服务项目，这里可用分布式事务解决事务回滚问题
        //这里我就直接做插入记录 有错误就回滚
     Detail detail = new Detail();
        detail.setCreateTime(new Date());
        detail.setDetailType(userVO.getType());
        detail.setUsername(wallet.getUsername());
        detail.setPrice(price);
//        int i= 1/0;
        detailService.save(detail);
        return R.ok();
    }



}


